package cn.smallran.novel.service.impl;

import java.util.List;
import cn.smallran.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.smallran.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.smallran.novel.domain.BookChapter;
import cn.smallran.novel.mapper.BookInfoMapper;
import cn.smallran.novel.domain.BookInfo;
import cn.smallran.novel.service.IBookInfoService;

/**
 * 小说列表Service业务层处理
 * 
 * @author smallran
 * @date 2024-12-01
 */
@Service
public class BookInfoServiceImpl implements IBookInfoService 
{
    @Autowired
    private BookInfoMapper bookInfoMapper;

    /**
     * 查询小说列表
     * 
     * @param id 小说列表主键
     * @return 小说列表
     */
    @Override
    public BookInfo selectBookInfoById(Long id)
    {
        return bookInfoMapper.selectBookInfoById(id);
    }

    /**
     * 查询小说列表列表
     * 
     * @param bookInfo 小说列表
     * @return 小说列表
     */
    @Override
    public List<BookInfo> selectBookInfoList(BookInfo bookInfo)
    {
        return bookInfoMapper.selectBookInfoList(bookInfo);
    }

    /**
     * 新增小说列表
     * 
     * @param bookInfo 小说列表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBookInfo(BookInfo bookInfo)
    {
        bookInfo.setCreateTime(DateUtils.getNowDate());
        int rows = bookInfoMapper.insertBookInfo(bookInfo);
        insertBookChapter(bookInfo);
        return rows;
    }

    /**
     * 修改小说列表
     * 
     * @param bookInfo 小说列表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBookInfo(BookInfo bookInfo)
    {
        bookInfo.setUpdateTime(DateUtils.getNowDate());
        bookInfoMapper.deleteBookChapterByBookId(bookInfo.getId());
        insertBookChapter(bookInfo);
        return bookInfoMapper.updateBookInfo(bookInfo);
    }

    /**
     * 批量删除小说列表
     * 
     * @param ids 需要删除的小说列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInfoByIds(Long[] ids)
    {
        bookInfoMapper.deleteBookChapterByBookIds(ids);
        return bookInfoMapper.deleteBookInfoByIds(ids);
    }

    /**
     * 删除小说列表信息
     * 
     * @param id 小说列表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBookInfoById(Long id)
    {
        bookInfoMapper.deleteBookChapterByBookId(id);
        return bookInfoMapper.deleteBookInfoById(id);
    }

    /**
     * 新增小说章节信息
     * 
     * @param bookInfo 小说列表对象
     */
    public void insertBookChapter(BookInfo bookInfo)
    {
        List<BookChapter> bookChapterList = bookInfo.getBookChapterList();
        Long id = bookInfo.getId();
        if (StringUtils.isNotNull(bookChapterList))
        {
            List<BookChapter> list = new ArrayList<BookChapter>();
            for (BookChapter bookChapter : bookChapterList)
            {
                bookChapter.setBookId(id);
                list.add(bookChapter);
            }
            if (list.size() > 0)
            {
                bookInfoMapper.batchBookChapter(list);
            }
        }
    }
}
